package site.xuqing.autohelperengine.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import site.xuqing.autohelperengine.R;
import site.xuqing.autohelperengine.permission.FloatWindowPermissionChecker;
import site.xuqing.autohelperengine.util.AuthorityUtil;


public class AuthorityActivity extends AppCompatActivity {
    private Button bt_overlay;
    private Button bt_accessibility;
    private LinearLayout overlay_layout,accessibility_layout;
    private boolean isNeedOverlayPermission=true,isNeedAccessibilityPermission=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authority);
        setTitle("授权");

        overlay_layout=findViewById(R.id.overlay_layout);
        accessibility_layout=findViewById(R.id.accessibility_layout);
        bt_overlay=findViewById(R.id.bt_overlay);
        bt_accessibility=findViewById(R.id.bt_accessibility);
        bt_overlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toOpenOverlayAuthority();
            }
        });
        bt_accessibility.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toOpenAccessibilityAuthority();
            }
        });

        isNeedAccessibilityPermission=getIntent().getBooleanExtra("isNeedAccessibilityPermission",true);
        isNeedOverlayPermission=getIntent().getBooleanExtra("isNeedOverlayPermission",true);
        if (!isNeedOverlayPermission){
            overlay_layout.setVisibility(View.GONE);
        }
        if (!isNeedAccessibilityPermission){
            accessibility_layout.setVisibility(View.GONE);
        }
    }

    public static void startActivity(Context context,boolean isNeedOverlayPermission,boolean isNeedAccessibilityPermission){
        context.startActivity(new Intent(context,AuthorityActivity.class)
                .putExtra("isNeedOverlayPermission",isNeedOverlayPermission)
                .putExtra("isNeedAccessibilityPermission",isNeedAccessibilityPermission).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateButtonStatus();
    }

    void toOpenOverlayAuthority() {
        AlertDialog dialog=new AlertDialog.Builder(this)
                .setTitle("设置悬浮窗")
                .setMessage("检测到您的手机没有授予显示悬浮窗权限，开启后才能正常使用此功能。")
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        FloatWindowPermissionChecker.tryJumpToPermissionPage(AuthorityActivity.this);
                    }
                })
                .create();
        dialog.show();
    }

    void toOpenAccessibilityAuthority() {
        AlertDialog dialog=new AlertDialog.Builder(this)
                .setTitle("开启辅助功能")
                .setMessage("设置-无障碍中开启”获取手机号“服务")
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                        startActivity(intent);
                    }
                })
                .create();
        dialog.show();
    }

    private void updateButtonStatus() {
        boolean hasOverlayPermission = AuthorityUtil.hasOverlayPermission(this);
        boolean isServiceRunning = AuthorityUtil.isServiceRunning(this);
        if (hasOverlayPermission) {
            bt_overlay.setText("已开启");
            bt_overlay.setClickable(false);
            bt_overlay.setTextColor(0xff81B113);
            bt_overlay.setBackground(null);
        } else {
            bt_overlay.setText("去开启");
            bt_overlay.setClickable(true);
            bt_overlay.setTextColor(0xffffffff);
        }

        if (isServiceRunning) {
            bt_accessibility.setText("已开启");
            bt_accessibility.setClickable(false);
            bt_accessibility.setTextColor(0xff81B113);
            bt_accessibility.setBackground(null);
        } else {
            bt_accessibility.setText("去开启");
            bt_accessibility.setClickable(true);
            bt_accessibility.setTextColor(0xffffffff);
        }

        if (isNeedOverlayPermission&&isNeedAccessibilityPermission){
            if (hasOverlayPermission && isServiceRunning) {
                finish();
            }
        }else if(isNeedOverlayPermission){
            if (hasOverlayPermission) {
                finish();
            }
        }else if (isNeedAccessibilityPermission){
            if (isServiceRunning) {
                finish();
            }
        }else{
            finish();
        }
    }


}

